<?php

namespace App\Http\Controllers\Core;

use Illuminate\Support\Facades\Session;

class HomeController extends BaseController
{
    var $member;

    //需要登陆验证
    public function __construct()
    {
        parent::__construct();
    }

    public function isLogin()
    {
        $member = Session::get('member');
        if ($member && isSet($member->id) && $member->id) {
            $this->member = $member;
            return true;
        }
        return false;
    }



    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data['id']] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][$data['id']] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    function birthday($birth)
    {
        list($by, $bm, $bd) = explode('-', $birth);
        $cm = date('n');
        $cd = date('j');
        $age = date('Y') - $by - 1;
        if ($cm > $bm || $cm == $bm && $cd > $bd) $age++;
        return $age;
    }

    protected function retJson($status = 1, $msg = '', $data = [])
    {
        return json_encode(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }


    /**
     *
     */
    public function getUserId()
    {
        $member = Session::get('member');
        if ($member && isSet($member->id) && $member->id) {
            return $member->id;
        }
        return 0;
    }

}